package net.goutalk.glcs.module.oa.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.module.oa.dto.UpdateScheduleDto;
import net.goutalk.glcs.module.oa.dto.AddScheduleDto;
import net.goutalk.glcs.module.oa.entity.Schedule;
import net.goutalk.glcs.module.oa.service.IScheduleService;
import net.goutalk.glcs.module.oa.vo.ScheduleListVo;
import net.goutalk.glcs.module.oa.vo.ScheduleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 日程管理 前端控制器
 * </p>
 *
 * @author zlf
 * @since 2022-06-16
 */
@RestController
@RequestMapping(GlobalConstant.OA_MODULE_PREFIX + "/schedule")
@Api(value = GlobalConstant.OA_MODULE_PREFIX + "/schedule", tags = "日程管理")
@AllArgsConstructor
public class ScheduleController {

    private final IScheduleService scheduleService;


    @GetMapping
    @ApiOperation(value = "获取日程管理列表")
    public R list() {
        List<Schedule> scheduleList = scheduleService.list(Wrappers.<Schedule>query().lambda()
                .eq(Schedule::getEnabledMark, 1)
                .eq(Schedule::getCreateUserId, StpUtil.getLoginIdAsLong())
                .orderByAsc(Schedule::getSortCode));
        List<ScheduleListVo> resultList = new ArrayList<>(scheduleList.size());
        for (Schedule schedule : scheduleList) {
            ScheduleListVo scheduleListVo = new ScheduleListVo();
            scheduleListVo.setId(schedule.getId());
            scheduleListVo.setTitle(schedule.getScheduleContent());
            String endTime = schedule.getEndTime();
            if (!StrUtil.isEmpty(endTime)) {
                String[] endTimeArray = StrUtil.splitToArray(endTime, ":");
                scheduleListVo.setEnd(schedule.getEndDate().withHour(Integer.parseInt(endTimeArray[0])).withMinute(Integer.parseInt(endTimeArray[1])).withSecond(Integer.parseInt(endTimeArray[2])));
            }
            String startTime = schedule.getStartTime();
            if (!StrUtil.isEmpty(startTime)) {
                String[] startTimeArray = StrUtil.splitToArray(endTime, ":");
                scheduleListVo.setStart(schedule.getStartDate().withHour(Integer.parseInt(startTimeArray[0])).withMinute(Integer.parseInt(startTimeArray[1])).withSecond(Integer.parseInt(startTimeArray[2])));
            }
            scheduleListVo.setAllDay(false);
            resultList.add(scheduleListVo);
        }
        return R.ok(resultList);
    }

    @PostMapping
    @ApiOperation(value = "添加日程管理")
    public R add(@RequestBody AddScheduleDto scheduleDto) {
        Schedule schedule = BeanUtil.toBean(scheduleDto, Schedule.class);
        //添加消息通知
        boolean save = scheduleService.save(schedule);
        return R.ok(save);
    }

    @PutMapping
    @ApiOperation(value = "修改日程管理")
    public R update(@RequestBody UpdateScheduleDto scheduleDto) {
        Schedule schedule = BeanUtil.toBean(scheduleDto, Schedule.class);
        return R.ok(scheduleService.updateById(schedule));
    }

    @DeleteMapping
    @ApiOperation(value = "删除日程管理")
    public R delete(@RequestBody List<Long> ids) {
        return R.ok(scheduleService.delete(ids));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "日程详情")
    public R info(@RequestParam Long id) {
        Schedule schedule = scheduleService.getById(id);
        if (schedule == null) {
            R.error("找不到当前日程！");
        }
        return R.ok(BeanUtil.toBean(schedule, ScheduleVo.class));
    }
}
